package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.ForYourCustomAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.ForYourCustomDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 26.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 * @desc 为你定制列表
 */
public class ForYourCustomActivity extends Activity {
    private static final String TAG = "ForYourCustomActivity";
    @Bind(R.id.activity_for_your_custom_iv_return)
    ImageView activityForYourCustomIvReturn;
    @Bind(R.id.activity_for_your_custom_recyclerview)
    RecyclerView activityForYourCustomRecyclerview;

    private int p1 = 1;
    private ForYourCustomAdapter forYourCustomAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_for_your_custom);
        ButterKnife.bind(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityForYourCustomRecyclerview.setLayoutManager(layoutManager);

        loadForYourCustomDate(p1);
    }
    /**
     * @desc 加载为你定制的数据
     * */
    private void loadForYourCustomDate(int p) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.PREGNANT_MOTHER_FOR_YOUR_CUSTOM)
                .addParams("state", "2")
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse =  response.body().string();
                        Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson =  new Gson();
                        final ForYourCustomDateBean forYourCustomDateBean =   gson.fromJson(parseNetworkResponse,ForYourCustomDateBean.class);
                        if(forYourCustomDateBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                   final List<ForYourCustomDateBean.DataBean> bean =  forYourCustomDateBean.data;
                                    if(bean != null){
                                        forYourCustomAdapter = new ForYourCustomAdapter(bean,ForYourCustomActivity.this);
                                        activityForYourCustomRecyclerview.setAdapter(forYourCustomAdapter);
                                        forYourCustomAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                                            @Override
                                            public void onItemClick(View view) {
                                             int  position =   activityForYourCustomRecyclerview.getChildAdapterPosition(view);
                                                Intent intent = new Intent(ForYourCustomActivity.this,ForYourCustomArtclePagerActivity.class);
                                                intent.putExtra("pager_id",bean.get(position).id);
                                                intent.putExtra("art_name",bean.get(position).art_name);
                                                startActivity(intent);

                                            }

                                            @Override
                                            public void onItemFirstButtonClick(View v, int position) {

                                            }
                                        });
                                    }
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(ForYourCustomActivity.this,forYourCustomDateBean.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @OnClick(R.id.activity_for_your_custom_iv_return)
    public void onViewClicked() {
        finish();
    }
}
